package com.seal.tsms.other.controller;

import com.seal.tsms.base.controller.BaseController;
import com.seal.tsms.base.service.BaseService;
import com.seal.tsms.other.entity.Subject;
import com.seal.tsms.other.entity.Token;
import com.seal.tsms.other.entity.Vote;
import com.seal.tsms.other.entity.VotePk;
import com.seal.tsms.other.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by seal on 2016/3/26.
 */
@Controller
@RequestMapping("/other/vote")
public class VoteController extends BaseController<Vote,VotePk> {
    @Override
    @Autowired
    @Qualifier("voteServiceImpl")
    public void setSelfService(BaseService<Vote, VotePk> selfService) {
        super.setSelfService(selfService);
    }
    @Autowired
    private SubjectService subjectService;
    @RequestMapping(value = "/vote",method = RequestMethod.POST)
    @ResponseBody
    public Map zhiChi(@RequestParam("subjectId")  String subjectId, HttpSession session){
        Map data=new HashMap();
        Vote vote=new Vote();
        Token token=(Token) session.getAttribute("token");
        if(token!=null){
        try {
            Subject subject=subjectService.get(subjectId);
            vote.setSubject(subject);
            vote.setToken(token);
            getSelfService().save(vote);
            data.put("state",true);
            data.put("msg",subject.getToken().getNickName()+" 感谢你的支持");
        }catch (Exception e){
            data.put("state",false);
            data.put("msg"," 你没有支持成功");
            log.error("支持失败",e);
        }}else{
            data.put("state",false);
            data.put("msg","很遗憾你可能还没登陆，支持失败");
        }
            return data;
    }
}
